<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="策略" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择策略">
          <el-option label="优先级1）成都所有订单" value="1"></el-option>
          <el-option label="优先级2）重庆携转订单" value="2"></el-option>
          <el-option label="优先级3）重庆正常订单" value="3"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="员工姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" type="input" placeholder="请输入员工姓名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
    <!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--      &lt;!&ndash;          type="primary"&ndash;&gt;-->
    <!--      &lt;!&ndash;          plain&ndash;&gt;-->
    <!--      &lt;!&ndash;          icon="el-icon-plus"&ndash;&gt;-->
    <!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--      &lt;!&ndash;          @click="handleAdd"&ndash;&gt;-->
    <!--      &lt;!&ndash;          v-hasPermi="['hmk:receive_order_config:add']"&ndash;&gt;-->
    <!--      &lt;!&ndash;        >新增&ndash;&gt;-->
    <!--      &lt;!&ndash;        </el-button>&ndash;&gt;-->
    <!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['hmk:receive_order_config:edit']"-->
    <!--        >修改-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['hmk:receive_order_config:remove']"-->
    <!--        >删除-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="warning"-->
    <!--          plain-->
    <!--          icon="el-icon-download"-->
    <!--          size="mini"-->
    <!--          @click="handleExport"-->
    <!--          v-hasPermi="['hmk:receive_order_config:export']"-->
    <!--        >导出-->
    <!--        </el-button>-->
    <!--      </el-col>-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="receive_order_configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="策略" align="center" prop="type">
        <template #default="{row}">
          <span>{{ typeFormat(row.type) }}
             <el-tooltip :content="typeTipFormat(row.type)" placement="top">
                  <el-image :src="require('@/png/帮助.png')">  </el-image>
            </el-tooltip>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="员工姓名" align="center" prop="nickName">
      </el-table-column>
      <el-table-column label="员工id" align="center" prop="userId"/>
      <el-table-column label="在途单量" align="center" prop="count"/>
      <el-table-column label="分单开关" align="center" prop="swith">
        <template #header>
          <span>分单开关
            <el-tooltip content="员工休假不需要分单时请在此关闭"
                        placement="top">
                     <el-image :src="require('@/png/帮助.png')">  </el-image>
                 </el-tooltip>
          </span>
        </template>
        <template #default="{row}">
          <el-switch v-model="row.swith" :active-value="activeValue" :inactive-value="inActiveValue"
                     @change="handleUpdate(row)"></el-switch>
        </template>
      </el-table-column>

      <el-table-column label="用户开关" align="center" prop="userSwitch">
        <template #header>
            <span>员工开关
              <el-tooltip content="员工可以通过后台主动关闭接单" placement="top">
                 <el-image :src="require('@/png/帮助.png')">  </el-image>
              </el-tooltip>
           </span>
        </template>

        <template #default="{row}">
          <el-switch focus="" v-model="row.userSwitch" :active-value="activeValue" :inactive-value="inActiveValue"
                     disabled/>
        </template>
      </el-table-column>

      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['hmk:receive_order_config:edit']"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['hmk:receive_order_config:remove']"-->
      <!--          >删除-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改自动接单配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="总开关">
          <el-switch
            v-model="switchValue"
            active-text="开"
            inactive-text="关">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addReceive_order_config,
  delReceive_order_config,
  listReceive_order_config,
  updateReceive_order_config
} from "@/api/hmk/receive_order_config";

export default {
  name: "Receive_order_config",
  dicts: ['is_receive_order',],
  data() {
    return {
      activeValue: 1,
      inActiveValue: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 自动接单配置表格数据
      receive_order_configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        userId: null,
        swith: null,
        type: null,
        userSwitch: null,
        nickName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          {required: true, message: "用户id不能为空", trigger: "blur"}
        ],
        swith: [
          {required: true, message: "0不开启1开启不能为空", trigger: "blur"}
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  computed: {
    switchValue: {
      get() {
        return this.form.swith == 1;
      },
      set(value) {
        this.form.swith = value ? 1 : 0
      }
    }
  },
  methods: {
    typeTipFormat(type) {
      if (type == 1) {
        return '订单商品ID为3759479174591545549/3754894490461143521/3754895224707613621/3754895639272620508/3712181777171874079/3758618087461617678/3758618261298741755/3758618385961845293/3739133422728249754/3761737603712417839/3763969474903015906，按在途订单（在途=待付款、未处理、处理中、待竣工、已实名、已超时且未激活）分配给接单人'
      } else if (type == 2) {
        return '订单类型为抖店订单的，关键字包含便民服务宽带套餐。收货区为渝中会分配给杨勇杏、巴南会分配给王雪梅、大渡口会分配给冉欣于、九龙坡/沙坪坝的会分给胡萍。剩下的平均分配给开启接单的接单人'
      } else if (type == 3) {
        return '订单类型为抖店订单的，按在途订单（在途=待付款、未处理、处理中、待竣工、已实名、已超时且未激活）分配给接单人'
      }
    },
    typeFormat(type) {
      if (type == 1) {
        return '优先级1）成都所有订单'
      } else if (type == 2) {
        return '优先级2）重庆携转订单'
      } else if (type == 3) {
        return '优先级3）重庆正常订单'
      }
    },
    /** 查询自动接单配置列表 */
    getList() {
      this.loading = true;
      listReceive_order_config(this.queryParams).then(response => {
        this.receive_order_configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        swith: true
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加自动接单配置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      updateReceive_order_config({'id': row.id, 'swith': row.swith}).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReceive_order_config(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReceive_order_config(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除自动接单配置编号为"' + ids + '"的数据项？').then(function () {
        return delReceive_order_config(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('hmk/receive_order_config/export', {
        ...this.queryParams
      }, `receive_order_config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
